package com.sds.sweng.swat.miplatform.service.layouts;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;

public class MiplatformServicePageLayout extends Layout {
	
	private final int DESIGN_AREA = 0;
	private final int CONTROL_AREA = 1;
	
	private int controlAreaWidth;
	
	public MiplatformServicePageLayout(int controlAreaWidth) {
		this.controlAreaWidth = controlAreaWidth;
	}

	@Override
	protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
		int width = Math.max(wHint, controlAreaWidth);
		int height = hHint;
		
		return new Point(width, height);
	}

	@Override
	protected void layout(Composite composite, boolean flushCache) {
		Rectangle rect = composite.getClientArea();
		Control[] children = composite.getChildren();
		int count = children.length;
		
		if (count < 2) return;
		
		int designAreaWidth = rect.width - controlAreaWidth;
		designAreaWidth = designAreaWidth < 0 ? 0 : designAreaWidth;
		int height = rect.height;
		
		Control designArea = children[DESIGN_AREA];
		designArea.setBounds(0, 0, designAreaWidth, height);
		
		Control controlArea = children[CONTROL_AREA];
		controlArea.setBounds(designAreaWidth, 0, controlAreaWidth, height);
	}

}
